/*
*	预加载插件
*/

;(function (jQuery) {
	'use strict';

	var Preload = function (resources, deferred) {
		var self = this;
		self.resources = resources;
		self.deferred = deferred;
		self.loaded = [];
    	self.failed = [];
    	self.start();
	};

	Preload.prototype = {
		constructor: Preload,

		// 开始预加载
		start: function () {
			var self = this;
			if (!self.resources.length) {
        		self.end();
      		}
      		for (var i = 0; i < self.resources.length; i ++) {
      			// 判断需要加载的文件属于那种格式
      			var format = self.resources[i].substring(self.resources[i].lastIndexOf('.') + 1).toLowerCase();
      			if ($.preload.mimetypes[format]) {
      				// 执行预加载过程
		        	self.process(self.resources[i], $.preload.mimetypes[format]);
		        } 
		        else {
		        	// 格式不存在
		        	self.error(self.resources[i]);
		        }
      		}
		},

		// 预加载结束
		end: function () {
			var self = this;
			if (self.loaded.length + self.failed.length >= self.resources.length) {
				if (!self.failed.length) {
		         	self.deferred.resolve(self.loaded, self.failed);
		        }
		        else {
		         	self.deferred.reject(self.loaded, self.failed);
		        }
			}
		},

		// 预加载过程
		process: function (resource, type) {
			var self = this;
			if (self[type] && typeof self[type] === 'function') {
				self[type](resource)
					.then(function () {
						// 预加载成功后将资源添加至预加载成功的数组中
			        	self.loaded.push(resource);
			        	self.end();
			        }, function () {
			        	// 预加载失败，将资源添加至预加载失败的数组中
			        	self.failed.push(resource);
			        	self.end();
			        });
			}
		},

		// 图片预加载
		image: function (resource) {
			var deferred = $.Deferred(), image = document.createElement('img');
          	// 图片预加载成功
          	image.addEventListener('load', function () {
        		deferred.resolve(resource);
        		image = null;
      		});
      		// 图片预加载失败
      		image.addEventListener('error', function () {
        		deferred.reject(resource);
        		image = null;
      		});
      		image.src = resource;
      		return deferred;
		},

		// 音频预加载
		audio: function (resource) {
			var deferred = $.Deferred(), audio = document.createElement('audio');
          	// 音频预加载成功
		    audio.addEventListener('canplaythrough', function () {
		        deferred.resolve(resource);
		        audio = null;
		    });
		    // 音频预加载失败
      		audio.addEventListener('error', function () {
        		deferred.reject(resource);
        		audio = null;
      		});
      		audio.src = resource;
      		return deferred;
		},

		// 视频预加载
		video: function (resource) {
      		var deferred = $.Deferred(), video = document.createElement('video');
          	// 视频预加载成功
			video.addEventListener('canplaythrough', function () {
				deferred.resolve(resource);
				video = null;
			});
			// 视频预加载失败
		    video.addEventListener('error', function () {
		        deferred.reject(resource);
		        video = null;
		    });
      		video.src = resource;
      		return deferred;
    	},
  
    	// 格式不存在时执行
    	error: function (resource) {
    		var self = this;
     		self.deferred.reject(resource);
    	}
	};

	$.preload = function (resources) {
	    if (typeof resources === 'string') {
	      resources = [resources];
	    }
	    var deferred = $.Deferred(),
	        preload = new Preload(resources , deferred);
	    return deferred;
  	};

	$.preload.mimetypes = {
	    bmp: 'image',
	    gif: 'image',
	    jpg: 'image',
	    jpeg: 'image',
	    png: 'image',
	    svg: 'image',
	    m4a: 'audio',
	    mp3: 'audio',
	    oga: 'audio',
	    ogg: 'audio',
	    opus: 'audio',
	    wav: 'audio',
	    weba: 'audio',
	    mp4: 'video',
	    m4v: 'video',
	    ogv: 'video',
	    webm: 'video',
  	};

}($));
